package com.gyh.stlserver.config;

import com.gyh.stlserver.interceptor.AdminInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private AdminInterceptor adminInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/fore/**",
                        "/upload/savePic",
                        "/captcha/**","/job/**",
                        "/admin/login","/admin/getLogin");
//        ,
//        "/course","/reserve/**","/charts/**",
//                "/swagger**/**","/**/api/-docs/**","/admin/**"
//                ,"/coach/**","/member/**","/course/**","/upload/**"
    }
    /*解决跨域问题*/
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 设置访问员地址
        corsConfiguration.addAllowedHeader("*");// 设置访问源请求头
        corsConfiguration.addAllowedMethod("*");// 设置访问源请求方法
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsFilter(source);
    }
}
